package com.wukaige.order.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.wukaige.order.entity.Order;
import com.wukaige.order.service.IOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @author wukaige
 * @date 2021/5/10 10:03
 */
@Slf4j
@Component
@EnableScheduling
public class OrderCancelTask {

    @Autowired
    private IOrderService iOrderService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void orderCancel() {
        //查询未支付订单
        List<Order> orderList = iOrderService.getListByStatus(1);
        orderList.forEach(order -> {
            //取消超时订单,1分钟超时
            DateTime dateTime = DateUtil.offsetMinute(order.getCretime(), 1);
            int compare = DateUtil.compare(new Date(), order.getCretime());
            if (compare > 0) {
                iOrderService.cancelOrder(order.getOid());
                log.info(order.getOid() + "取消成功");
            }
        });

    }

}
